<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>运营工具</cite></a>
    <a><cite>自动锁单</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_lock_task_search">
        <script type="text/html" template lay-done="layui.data.done()">
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{layui.router().search.keyword || ''}}" class="layui-input"
                     placeholder="请输入关键字">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_lock_task_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_lock_task_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
            <button id="btn_add_lock_task" class="layui-btn layui-btn-normal">
              <i class="layui-icon layui-icon-add-1"></i>添加
            </button>
          </div>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_lock_task"></div>
      <script type="text/html" id="tpl_time">
        {{d.start_time}} ~ {{d.end_time}}
      </script>
      <script type="text/html" id="tpl_price">
        {{d.display_min_price}} ~ {{d.display_max_price}}
      </script>
      <script type="text/html" id="tpl_tool">
        <a class="layui-table-link layui-font-green" lay-event="edit">
          <i class="layui-icon layui-icon-edit"></i>
        </a>
        <div class="ant-divider"></div>
        <a class="layui-table-link layui-font-red" lay-event="del">
          <i class="layui-icon layui-icon-delete"></i>
        </a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.data.done = function () {
    layui.use(['fox_table'], function () {
      let $ = layui.$
        , fox_table = layui.fox_table
        , admin = layui.admin
        , form = layui.form
        , view = layui.view

      let resourceUrl = 'agent-admin/business/auto_lock_task'

      // 列表查询
      fox_table.render({
        id: 'table_lock_task'
        , url: resourceUrl
        , cols: [[
          {title: 'ID', field: 'id', width: 80, align: "center", sort: true}
          , {title: '监测产品', field: 'product_name', minWidth: 100}
          , {title: '锁单账户', field: 'locker_account', width: 120, align: "center"}
          , {title: '锁定时间', field: 'lock_minutes', width: 100, align: "center"}
          , {title: '运行时间', templet: '#tpl_time', width: 320, align: "center"}
          , {title: '价格区间', templet: '#tpl_price', width: 180, align: "center"}
          , {title: '锁定数量', field: 'locked_count', width: 100, align: "center"}
          , {title: '操作', width: 100, align: 'left', fixed: 'right', toolbar: '#tpl_tool'}
        ]]
      });

      //监听操作栏
      fox_table.toolbar(function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
          showForm(data)
        }
        if (obj.event === 'del') {
          layer.confirm('确认删除该数据?', function (index) {
            admin.del(resourceUrl, data.id, function (res) {
              layer.close(index);
              fox_table.reload(); //重载表格
            })
          });
        }
      });
      //搜索
      form.on('submit(submit_lock_task_search)', function (d) {
        fox_table.reload(d.field);
        return false;
      });
      //重置
      form.on('submit(reset_lock_task_search)', function (d) {
        $("input[name='keyword']").val('');
        fox_table.reload({});
        return false;
      });
      //添加
      $('#btn_add_lock_task').click(function () {
        showForm();
        return false;
      });

      //显示弹窗
      function showForm(data) {
        data = data || {};
        let minWidth = '500px';
        let minHeight = '500px';
        admin.popup({
          title: (data.id ? '修改' : '添加') + '自动锁单任务'
          , area: [minWidth, minHeight]
          , id: 'popup_lock_task'
          , success: function (layero, index) {
            view(this.id).render('business/lock_task_form', data).done(function () {
              form.on('submit(submit_lock_task)', function (d) {
                let field = d.field;
                admin.post(resourceUrl, field, function (res) {
                  layer.close(index);
                  fox_table.reload();
                })
              })
            })
          }
        })
      }
    })
  }
</script>

